<?php 
    $arrActive = array(
    '0'=> '',
    '1'=> 'checked="true"',
    ''=>''
    );
    
    $navigation = '';
    $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
    foreach($this->dataSources as $item){
        if($item['description'] !=""){
            $keys = array_keys($item['controller']);
            $navigation .= '<optgroup label="MODULE '.strtoupper($item['description']).'" style="color:#B8B8B8;font-style:normal">';
            for($i=0;$i<count($keys);$i++)
            {
                $description = $item['controller'][$keys[$i]]['description'];
                
                $des = '';
                if(strtoupper($description) != strtoupper($keys[$i])){
                    $des = ' : '.$description;
                }
                
                $navigation .= '<optgroup label="'.$tab.strtolower($keys[$i]).$des.'" value="'.$keys[$i].'" style="color:#004FA0;font-style:normal">';
                $arr_action = $item['controller'][$keys[$i]]['action'];
                $keyAction = array_keys($arr_action);
                for($ii=0;$ii<count($keyAction);$ii++)
                {
                    $desAction = '';
                    if(strtoupper($arr_action[$keyAction[$ii]]) != strtoupper($keyAction[$ii])){
                        $desAction = ' : '.$arr_action[$keyAction[$ii]];
                    }
                    $navigation .= '<option value="'.$item['description'].'/'.$keys[$i].'/'.$keyAction[$ii].'" name="'.$item['description'].'_'.$keys[$i].'_'.$keyAction[$ii].'" style="color:#393939;">'.$tab.$tab.$tab.$keyAction[$ii].$desAction.'</option>';
                }
                $navigation .= "</optgroup>";
            }
            $navigation .= "</optgroup>";
        }
    }
?>

<form name="frm" action="/admin/roles/save" method="POST">
<table border="0" cellpadding="0" cellspacing="0"  id="id-form">
    <tr>
        <th valign="top">Tên vai trò:</th>
        <td><input type="text" class="inp-form" name="NAME" value="<?=$this->data['NAME']?>" /></td>
        <td></td>
    </tr>
    <tr>
        <th valign="top">Mã vai trò:</th>
        <td><input type="text" class="inp-form" name="CODE" value="<?=$this->data['CODE']?>" /></td>
        <td>
            <div class="error-left" style="display:none" id="errCodePad"></div>
            <div class="error-inner" style="display:none" id="errCode" >Bạn phải nhập mã vai trò</div>
        </td>
    </tr>
    <tr>
        <th valign="top">Điều hướng:</th>
        <td>
            <select name="NAVIGATION" id="navigation" value="" > 
                <?php
                
                   $navigation = '';
                    $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
                    foreach($this->dataSources as $item1){
                        if($item1['description'] !=""){
                            $keys = array_keys($item1['controller']);
                            $navigation .= '<optgroup label="MODULE '.strtoupper($item1['description']).'" style="color:#B8B8B8;font-style:normal">';
                            for($i=0;$i<count($keys);$i++)
                            {
                                $description = $item1['controller'][$keys[$i]]['description'];
                                
                                $des = '';
                                if(strtoupper($description) != strtoupper($keys[$i])){
                                    $des = ' : '.$description;
                                }
                                
                                $navigation .= '<optgroup label="'.$tab.strtolower($keys[$i]).$des.'" value="'.$keys[$i].'" style="color:#004FA0;font-style:normal">';
                                $arr_action = $item1['controller'][$keys[$i]]['action'];
                                $keyAction = array_keys($arr_action);
                                for($ii=0;$ii<count($keyAction);$ii++)
                                {
                                    $selected = "";
                                    $value = $item1['description'].'/'.$keys[$i].'/'.$keyAction[$ii];
                                    if($this->data['NAVIGATION'] == $value){
                                        $selected = "selected ='true' ";
                                    }
                                    $desAction = '';
                                    if(strtoupper($arr_action[$keyAction[$ii]]) != strtoupper($keyAction[$ii])){
                                        $desAction = ' : '.$arr_action[$keyAction[$ii]];
                                    }
                                    $navigation .= '<option value="'.$value.'" name="'.$item1['description'].'_'.$keys[$i].'_'.$keyAction[$ii].'" style="color:#393939;" '.$selected.' >'.$tab.$tab.$tab.$keyAction[$ii].$desAction.'</option>';
                                }
                                $navigation .= "</optgroup>";
                            }
                            $navigation .= "</optgroup>";
                        }
                    }
                   echo $navigation;
                   ?>
            </select>
        </td>
        <td>
            <div class="error-left" style="display:none" id="errCodePad"></div>
            <div class="error-inner" style="display:none" id="errCode" >Bạn phải nhập mã vai trò</div>
        </td>
    </tr>
    <tr>
        <th valign="top">Kích hoạt:</th>
        <td>
            <input type="checkbox" name="STATUS" value='1' <?=$arrActive[$this->data['STATUS']]?> />
        </td>
        <td></td>
    </tr>
    <tr>
        <th>&nbsp;</th>
        <td valign="top">
            <input type="button" value="" class="form-submit" onclick="save()" />
            <input type="reset" value="" class="form-reset" onclick="document.frm.reset()" />
        </td>
        <td></td>
    </tr>
</table>
<input type="hidden" name="ISUPDATE" value="<?=$this->data['CODE']?>" />
<input type="hidden" name="CODEOLD" value="<?=$this->data['CODE']?>" />
</form>
<script>

function save(){
    common.submitForm(document.frm,'success');
}
function success(){
    if(rs == '01'){
    common.loadAjaxPushState('/admin/roles/index');
    }else if(rs == '02'){
        document.getElementById('errCodePad').style.display = 'block';
        document.getElementById('errCode').style.display = 'block';
        document.getElementById('errCode').innerHTML = 'Mã vai trò đã tồn tại';
    }else if(rs == '03'){
        alert('Lỗi hệ thống');
    }
}
function back(){
    common.loadAjaxPushState('/admin/roles/index');
}

selectNavigation();
function selectNavigation(){
    var currentNav = "<?=$this->data['NAVIGATION']?>";
    if(currentNav != ""){
        var arrNavi = currentNav.split('/'); 
        var navigation = arrNavi[0]+'_'+arrNavi[1]+'_'+arrNavi[2];
        var box = document.getElementById('navigation').getElementsByTagName('option');
        for(var ii = 0 ; ii< box.length;ii++){
            if(box[ii].getAttribute('name') == navigation){
               box[ii].setAttribute('selected',true);
            }
        }
    }
}
</script>